package com.ccrfid.rmc6.service.business.huxi;

import com.ccrfid.rmc6.entity.personnel.PersonnelView;
import com.ccrfid.rmc6.entity.sys.KeyValueEntity;
import com.ccrfid.rmc6.pojo.Constants;
import com.ccrfid.rmc6.pojo.huxi.ConfigItem;
import com.ccrfid.rmc6.pojo.huxi.TabConfig;
import com.ccrfid.rmc6.pojo.huxi.TabData;
import com.ccrfid.rmc6.repository.personnel.PersonnelViewRepository;
import com.ccrfid.rmc6.repository.sys.KeyValueRepository;
import com.ccrfid.rmc6.util.GeomUtil;
import com.ccrfid.rmc6.util.JsonUtil;
import com.fasterxml.jackson.core.type.TypeReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.PostConstruct;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class HuxiDataServiceImpl implements HuxiDataService {
    private static final Logger logger = LoggerFactory.getLogger(HuxiDataServiceImpl.class);

    private static final String dataKey = "huxi-data-config";

    @Autowired
    private KeyValueRepository keyValueRepository;
    @Autowired
    private PersonnelViewRepository personnelViewRepository;

    @PostConstruct
    private void init() {
        List<TabConfig> configList = getConfig();
        logger.debug("** configList: {}", JsonUtil.INSTANCE.writeValueAsString(configList));
    }

    @Override
    public List<TabData> getTabDataList(Integer personnelType) {
        List<PersonnelView> list;
        if (personnelType != null) {
            list = personnelViewRepository.getAllByEnabledAndPersonnelType(Constants.ENABLE_STATUS.ENABLE.id, personnelType);
        } else {
            list = personnelViewRepository.getAllByEnabled(Constants.ENABLE_STATUS.ENABLE.id);
        }
        List<TabConfig> configList = getConfig();
        if (configList != null && list != null) {
            list.stream().filter(personnelView ->
                            personnelView.getPos() != null && personnelView.getFloorNumber() != null)
                    .forEach(personnelView ->
                            configList.stream().forEach(config ->
                                    config.getItems().stream()
                                            .filter(configItem -> configItem.getFloor().equals(personnelView.getFloorNumber())
                                                    && configItem.getPolygon().contains(personnelView.getPos()))
                                            .forEach(ConfigItem::incCount)
                            )
                    );
            List<TabData> dataList = configList.stream()
                    .map(TabConfig::toTabData)
                    .collect(Collectors.toList());
            return dataList;
        }
        return new ArrayList<>();
    }

    @Override
    public List<TabConfig> getConfig() {
        KeyValueEntity entity = keyValueRepository.getByK(dataKey);
        if (entity != null && StringUtils.hasLength(entity.getV())) {
            return JsonUtil.INSTANCE.readValue(entity.getV(), new TypeReference<List<TabConfig>>() {
            });
        } else {
            return resetConfig();
        }
    }

    @Override
    public List<TabConfig> updateConfig(List<TabConfig> configList) {
        if (configList != null && configList.size() > 0) {
            String content = JsonUtil.INSTANCE.writeValueAsString(configList);
            KeyValueEntity entity = new KeyValueEntity(dataKey, content);
            keyValueRepository.save(entity);
        }
        return configList;
    }

    @Override
    public List<TabConfig> resetConfig() {
        List<TabConfig> list = new ArrayList<>();
        list.add(default1());
        list.add(default2());
        list.add(default3());
        list.add(default4());
        list.add(default5());
        list.add(default6());
        list.add(default7());
        list.add(default8());
        list.add(default9());
        list.add(default10());
        list.add(default11());
        list.add(default12());
        list.add(default13());
        list.add(default14());
        list.add(default15());
        list.add(default16());
        list.add(default17());
        list.add(default18());
        list.add(default19());
        String content = JsonUtil.INSTANCE.writeValueAsString(list);
        KeyValueEntity entity = new KeyValueEntity(dataKey, content);
        keyValueRepository.save(entity);
        return list;
    }

    private TabConfig default1() {
        List<ConfigItem> items = new ArrayList<>();
        items.add(new ConfigItem("一层", 1, GeomUtil.INSTANCE.createPolygon(455, 108, 816, 217)));
        items.add(new ConfigItem("二层", 2, GeomUtil.INSTANCE.createPolygon(455, 108, 816, 217)));
        items.add(new ConfigItem("三层", 3, GeomUtil.INSTANCE.createPolygon(455, 108, 816, 217)));
        return new TabConfig("三号车间", 0, GeomUtil.INSTANCE.createPoint(352.0, 223.0), items);
    }

    private TabConfig default2() {
        List<ConfigItem> items = new ArrayList<>();
        items.add(new ConfigItem("一层", 1, GeomUtil.INSTANCE.createPolygon(455, 280, 816, 390)));
        items.add(new ConfigItem("二层", 2, GeomUtil.INSTANCE.createPolygon(455, 280, 816, 390)));
        items.add(new ConfigItem("三层", 3, GeomUtil.INSTANCE.createPolygon(455, 280, 816, 390)));
        return new TabConfig("二号车间", 0, GeomUtil.INSTANCE.createPoint(335.0, 335.0), items);
    }

    private TabConfig default3() {
        List<ConfigItem> items = new ArrayList<>();
        items.add(new ConfigItem("一层", 1, GeomUtil.INSTANCE.createPolygon(455, 460, 816, 570)));
        items.add(new ConfigItem("二层", 2, GeomUtil.INSTANCE.createPolygon(455, 460, 816, 570)));
        items.add(new ConfigItem("三层", 3, GeomUtil.INSTANCE.createPolygon(455, 460, 816, 570)));
        return new TabConfig("一号车间", 0, GeomUtil.INSTANCE.createPoint(324.0, 450.0), items);
    }

    private TabConfig default4() {
        List<ConfigItem> items = new ArrayList<>();
        items.add(new ConfigItem("一层", 1, GeomUtil.INSTANCE.createPolygon(895, 102, 1068, 205)));
        items.add(new ConfigItem("二层", 2, GeomUtil.INSTANCE.createPolygon(895, 102, 1068, 205)));
        items.add(new ConfigItem("三层", 3, GeomUtil.INSTANCE.createPolygon(895, 102, 1068, 205)));
        items.add(new ConfigItem("四层", 4, GeomUtil.INSTANCE.createPolygon(895, 102, 1068, 205)));
        return new TabConfig("四监区", 0, GeomUtil.INSTANCE.createPoint(891.0, 189.0), items);
    }

    private TabConfig default5() {
        List<ConfigItem> items = new ArrayList<>();
        items.add(new ConfigItem("一层", 1, GeomUtil.INSTANCE.createPolygon(895, 283, 1068, 386)));
        items.add(new ConfigItem("二层", 2, GeomUtil.INSTANCE.createPolygon(895, 283, 1068, 386)));
        items.add(new ConfigItem("三层", 3, GeomUtil.INSTANCE.createPolygon(895, 283, 1068, 386)));
        items.add(new ConfigItem("四层", 4, GeomUtil.INSTANCE.createPolygon(895, 283, 1068, 386)));
        return new TabConfig("五监区", 0, GeomUtil.INSTANCE.createPoint(869.0, 291.0), items);
    }

    private TabConfig default6() {
        List<ConfigItem> items = new ArrayList<>();
        items.add(new ConfigItem("一层", 1, GeomUtil.INSTANCE.createPolygon(895, 464, 1068, 566)));
        items.add(new ConfigItem("二层", 2, GeomUtil.INSTANCE.createPolygon(895, 464, 1068, 566)));
        items.add(new ConfigItem("三层", 3, GeomUtil.INSTANCE.createPolygon(895, 464, 1068, 566)));
        items.add(new ConfigItem("四层", 4, GeomUtil.INSTANCE.createPolygon(895, 464, 1068, 566)));
        return new TabConfig("三监区", 0, GeomUtil.INSTANCE.createPoint(850.0, 404.0), items);
    }

    private TabConfig default7() {
        List<ConfigItem> items = new ArrayList<>();
        items.add(new ConfigItem("一层", 1, GeomUtil.INSTANCE.createPolygon(895, 644, 1068, 747)));
        items.add(new ConfigItem("二层", 2, GeomUtil.INSTANCE.createPolygon(895, 644, 1068, 747)));
        items.add(new ConfigItem("三层", 3, GeomUtil.INSTANCE.createPolygon(895, 644, 1068, 747)));
        items.add(new ConfigItem("四层", 4, GeomUtil.INSTANCE.createPolygon(895, 644, 1068, 747)));
        return new TabConfig("九监区", 0, GeomUtil.INSTANCE.createPoint(829.0, 519.0), items);
    }

    private TabConfig default8() {
        List<ConfigItem> items = new ArrayList<>();
        items.add(new ConfigItem("一层", 1, GeomUtil.INSTANCE.createPolygon(1071, 102, 1244, 205)));
        items.add(new ConfigItem("二层", 2, GeomUtil.INSTANCE.createPolygon(1071, 102, 1244, 205)));
        items.add(new ConfigItem("三层", 3, GeomUtil.INSTANCE.createPolygon(1071, 102, 1244, 205)));
        items.add(new ConfigItem("四层", 4, GeomUtil.INSTANCE.createPolygon(1071, 102, 1244, 205)));
        return new TabConfig("七监区", 1, GeomUtil.INSTANCE.createPoint(1138.0, 194.0), items);
    }

    private TabConfig default9() {
        List<ConfigItem> items = new ArrayList<>();
        items.add(new ConfigItem("一层", 1, GeomUtil.INSTANCE.createPolygon(1071, 283, 1244, 386)));
        items.add(new ConfigItem("二层", 2, GeomUtil.INSTANCE.createPolygon(1071, 283, 1244, 386)));
        items.add(new ConfigItem("三层", 3, GeomUtil.INSTANCE.createPolygon(1071, 283, 1244, 386)));
        items.add(new ConfigItem("四层", 4, GeomUtil.INSTANCE.createPolygon(1071, 283, 1244, 386)));
        return new TabConfig("六监区", 1, GeomUtil.INSTANCE.createPoint(1165.0, 291.0), items);
    }

    private TabConfig default10() {
        List<ConfigItem> items = new ArrayList<>();
        items.add(new ConfigItem("一层", 1, GeomUtil.INSTANCE.createPolygon(1071, 464, 1244, 566)));
        items.add(new ConfigItem("二层", 2, GeomUtil.INSTANCE.createPolygon(1071, 464, 1244, 566)));
        items.add(new ConfigItem("三层", 3, GeomUtil.INSTANCE.createPolygon(1071, 464, 1244, 566)));
        items.add(new ConfigItem("四层", 4, GeomUtil.INSTANCE.createPolygon(1071, 464, 1244, 566)));
        return new TabConfig("一监区", 1, GeomUtil.INSTANCE.createPoint(1177.0, 405.0), items);
    }

    private TabConfig default11() {
        List<ConfigItem> items = new ArrayList<>();
        items.add(new ConfigItem("一层", 1, GeomUtil.INSTANCE.createPolygon(1071, 644, 1244, 747)));
        items.add(new ConfigItem("二层", 2, GeomUtil.INSTANCE.createPolygon(1071, 644, 1244, 747)));
        items.add(new ConfigItem("三层", 3, GeomUtil.INSTANCE.createPolygon(1071, 644, 1244, 747)));
        items.add(new ConfigItem("四层", 4, GeomUtil.INSTANCE.createPolygon(1071, 644, 1244, 747)));
        return new TabConfig("八监区", 1, GeomUtil.INSTANCE.createPoint(1192.0, 521.0), items);
    }

    private TabConfig default12() {
        List<ConfigItem> items = new ArrayList<>();
        items.add(new ConfigItem("一层", 1, GeomUtil.INSTANCE.createPolygon(891, 1014, 994, 1200)));
        items.add(new ConfigItem("二层", 2, GeomUtil.INSTANCE.createPolygon(891, 1014, 994, 1200)));
        items.add(new ConfigItem("三层", 3, GeomUtil.INSTANCE.createPolygon(891, 1014, 994, 1200)));
        return new TabConfig("会见楼", 0, GeomUtil.INSTANCE.createPoint(791.0, 782.0), items);
    }

    private TabConfig default13() {
        List<ConfigItem> items = new ArrayList<>();
        items.add(new ConfigItem("一层", 1, GeomUtil.INSTANCE.createPolygon(1376, 107, 1573, 206)));
        items.add(new ConfigItem("二层", 2, GeomUtil.INSTANCE.createPolygon(1376, 107, 1573, 206)));
        return new TabConfig("紧闭室", 1, GeomUtil.INSTANCE.createPoint(1536.0, 225.0), items);
    }

    private TabConfig default14() {
        List<ConfigItem> items = new ArrayList<>();
        items.add(new ConfigItem("一层", 1, GeomUtil.INSTANCE.createPolygon(1354, 252, 1600, 457)));
        return new TabConfig("食堂", 1, GeomUtil.INSTANCE.createPoint(1588.0, 342.0), items);
    }

    private TabConfig default15() {
        List<ConfigItem> items = new ArrayList<>();
        items.add(new ConfigItem("一层", 1, GeomUtil.INSTANCE.createPolygon(1324, 605, 1605, 726)));
        items.add(new ConfigItem("二层", 2, GeomUtil.INSTANCE.createPolygon(1324, 605, 1605, 726)));
        items.add(new ConfigItem("三层", 4, GeomUtil.INSTANCE.createPolygon(1324, 605, 1605, 726)));
        return new TabConfig("高戒备管理区", 1, GeomUtil.INSTANCE.createPoint(1609.0, 489.0), items);
    }

    private TabConfig default16() {
        List<ConfigItem> items = new ArrayList<>();
        items.add(new ConfigItem("一层", 1, GeomUtil.INSTANCE.createPolygon(1322, 904, 1610, 1007)));
        items.add(new ConfigItem("二层", 2, GeomUtil.INSTANCE.createPolygon(1322, 904, 1610, 1007)));
        items.add(new ConfigItem("三层", 3, GeomUtil.INSTANCE.createPolygon(1322, 904, 1610, 1007)));
        items.add(new ConfigItem("四层", 4, GeomUtil.INSTANCE.createPolygon(1322, 904, 1610, 1007)));
        return new TabConfig("原七监区", 1, GeomUtil.INSTANCE.createPoint(1609.0, 660.0), items);
    }

    private TabConfig default17() {
        List<ConfigItem> items = new ArrayList<>();
        items.add(new ConfigItem("一层", 1, GeomUtil.INSTANCE.createPolygon(1320, 1108, 1626, 1200)));
        items.add(new ConfigItem("二层", 2, GeomUtil.INSTANCE.createPolygon(1320, 1108, 1626, 1200)));
        items.add(new ConfigItem("三层", 3, GeomUtil.INSTANCE.createPolygon(1320, 1108, 1626, 1200)));
        items.add(new ConfigItem("四层", 4, GeomUtil.INSTANCE.createPolygon(1320, 1108, 1626, 1200)));
        return new TabConfig("医院", 1, GeomUtil.INSTANCE.createPoint(1618.0, 788.0), items);
    }

    private TabConfig default18() {
        List<ConfigItem> items = new ArrayList<>();
        items.add(new ConfigItem("一层", 1, GeomUtil.INSTANCE.createPolygon(993, 1040, 1239, 1200)));
        return new TabConfig("教培中心", 1, GeomUtil.INSTANCE.createPoint(1180.0, 782.0), items);
    }

    private TabConfig default19() {
        List<ConfigItem> items = new ArrayList<>();
        items.add(new ConfigItem("一层", 1, GeomUtil.INSTANCE.createPolygon(430, 1160, 645, 1235)));
        return new TabConfig("外管楼", 0, GeomUtil.INSTANCE.createPoint(283.0, 872.0), items);
    }

}
